﻿using RXY_Mall.Application.Order;
using RXY_Mall.Application.Order.Dto;
using RXY_Mall.Application.Return;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace RXY_Mall.Admin.Controllers
{
    public class OrderController : BaseController    
    {
        private IOrderService orderService;
        private IReturnService returnService;

        public OrderController()
        {
            orderService = new OrderService();
            returnService = new ReturnService();
        }

        #region 订单列表
        [HttpGet]
        public ActionResult List()
        {
            return View();
        }

        /// <summary>
        /// 修改运单
        /// </summary>
        [ChildActionOnly]
        public virtual ActionResult GetLogistics()
        {
            var result = orderService.GetLogistics();
            ViewBag.Logistics = result.data;

            return PartialView();
        }

        [HttpGet]
        public JsonResult GetOrders(string sEcho, int iDisplayStart, int iDisplayLength, string receiver_name, string receiver_mobile, string shop_name, string order_no, string start_date, string end_date, string user_no, int order_status = 0, int pay_status = 0, int shipment_status = 0, int pay_type = 0)
        {
            var result = orderService.GetOrders(receiver_name, receiver_mobile, shop_name, order_no, start_date, end_date, order_status, pay_status, shipment_status, pay_type, user_no, iDisplayStart / iDisplayLength + 1, iDisplayLength);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new { sEcho = sEcho, iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public async Task<JsonResult> GetOrderInfo()
        {
            var result = await orderService.GetOrderInfo(0);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> UpdateLogistics(UpdateLogisticsInput input)
        {
            if (ModelState.IsValid)
            {
                var result = await orderService.UpdateLogistics(input);
                if (result.code != 1)
                {
                    ErrorNotification(result.msg);
                }
                else
                {
                    SuccessNotification(result.msg);
                }
            }
            return RedirectToAction("GoodsOrder", new { id = input.order_id });
        }

        [HttpGet]
        public async Task<ActionResult> GoodsOrder(int id)
        {
            var result = await orderService.GetOrder(id);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
                return RedirectToAction("GoodsOrders");
            }
            return View(result.data);
        }

        /// <summary>
        /// 打印面单
        /// </summary>
        public async Task<ActionResult> PrintExpress(int id)
        {
            var result = await orderService.GetOrder(id);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
                return RedirectToAction("GoodsOrders");
            }
            return View(result.data);
        }

        [HttpGet]
        public JsonResult ExportOrders(string receiver_name, string receiver_mobile, string shop_name, string order_no, string start_date, string end_date, string user_no, int order_status = 0, int pay_status = 0, int shipment_status = 0, int pay_type = 0)
        {
            var result = orderService.ExportOrders(receiver_name, receiver_mobile, shop_name, order_no, start_date, end_date, order_status, pay_status, shipment_status, pay_type, user_no);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 拼手气
        [HttpGet]
        public ActionResult LuckGroupOrders()
        {
            return View();
        }

        [HttpGet]
        public JsonResult GetOrderGroups(string sEcho, int iDisplayStart, int iDisplayLength, string start_date, string end_date, string group_no, int group_status = 0)
        {
            var result = orderService.GetOrderGroups(group_no, start_date, end_date, group_status, iDisplayStart / iDisplayLength + 1, iDisplayLength);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new { sEcho = sEcho, iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public async Task<ActionResult> LuckGroupOrder(int id)
        {
            var result = await orderService.GetOrderGroup(id);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
                return RedirectToAction("LuckGroupOrders");
            }
            return View(result.data);
        }
        #endregion

        #region 免单
        [HttpGet]
        public ActionResult FreeGroupOrders()
        {
            return View();
        }

        [HttpGet]
        public JsonResult GetOrderFreeGroups(string sEcho, int iDisplayStart, int iDisplayLength, string start_date, string end_date, string group_no,string order_no, int group_status = 0)
        {
            var result = orderService.GetOrderFreeGroups(group_no, order_no, start_date, end_date, group_status, iDisplayStart / iDisplayLength + 1, iDisplayLength);
            if (result.code == 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new { sEcho = sEcho, iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public async Task<ActionResult> FreeGroupOrder(int id)
        {
            var result = await orderService.GetOrderFreeGroup(id);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
                return RedirectToAction("FreeGroupOrders");
            }
            return View(result.data);
        }
        #endregion

        #region 一元购
        [HttpGet]
        public ActionResult WelfareGroupOrders()
        {
            return View();
        }

        [HttpGet]
        public JsonResult GetOrderWelfareGroups(string sEcho, int iDisplayStart, int iDisplayLength, string start_date, string end_date, string group_no, string order_no, int group_status = 0)
        {
            var result = orderService.GetOrderWelfareGroups(group_no, order_no, start_date, end_date, group_status, iDisplayStart / iDisplayLength + 1, iDisplayLength);
            if (result.code == 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new { sEcho = sEcho, iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public async Task<ActionResult> WelfareGroupOrder(int id)
        {
            var result = await orderService.GetOrderWelfareGroup(id);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
                return RedirectToAction("WelfareGroupOrders");
            }
            return View(result.data);
        }
        #endregion

        #region 退换货
        [HttpGet]
        public ActionResult ReturnOrders()
        {
            return View();
        }

        [HttpGet]
        public JsonResult GetReturns(string sEcho, int iDisplayStart, int iDisplayLength, string return_no, string contact_mobile, string shop_name, string start_date, string end_date, int return_status = 0, int return_type = 0, int return_reason = 0)
        {
            var result = returnService.GetReturns(return_no, contact_mobile, shop_name, return_type, return_reason, start_date, end_date, return_status, iDisplayStart / iDisplayLength + 1, iDisplayLength);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new { sEcho = sEcho, iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public async Task<ActionResult> ReturnOrder(int id)
        {
            var result = await returnService.GetReturn(id);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
                return RedirectToAction("ReturnOrders");
            }
            var result2 = orderService.GetLogistics();
            ViewBag.Logistics = result2.data;
            return View(result.data);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> UpdateReturnAudit(UpdateReturnAuditInput input)
        {
            if (ModelState.IsValid)
            {
                var result = await returnService.UpdateReturnAudit(input);
                if (result.code != 1)
                {
                    ErrorNotification(result.msg);
                }
                else
                {
                    SuccessNotification(result.msg);
                }
            }
            return RedirectToAction("ReturnOrder", new { id = input.return_id });
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> UpdateReturnRefund(UpdateReturnRefundInput input)
        {
            if (ModelState.IsValid)
            {
                var result = await returnService.UpdateReturnRefund(input);
                if (result.code != 1)
                {
                    ErrorNotification(result.msg);
                }
                else
                {
                    SuccessNotification(result.msg);
                }
            }
            return RedirectToAction("ReturnOrder", new { id = input.return_id });
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> UpdateReturnShipment(UpdateReturnShipmentInput input)
        {
            if (ModelState.IsValid)
            {
                var result = await returnService.UpdateReturnShipment(input);
                if (result.code != 1)
                {
                    ErrorNotification(result.msg);
                }
                else
                {
                    SuccessNotification(result.msg);
                }
            }
            return RedirectToAction("ReturnOrder", new { id = input.return_id });
        }
        #endregion



    }
}